package com.hpin.fansea.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.PropertyResBean;
import com.hpin.fansea.utils.CommonUtils;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

import java.io.Serializable;

/**
 * 物业缴费 缴费页面
 * Created by Aaron on 15/9/14.
 */
public class PaymentActivity extends BaseActivity {

    private ImageView iv_personal;
    private ImageView iv_unit;
    private TextView tv_pay_community;
    private TextView tv_roomnum;
    private EditText et_contact;
    private EditText et_contact_tel;
    private EditText et_unit_name;
    //物业费信息
    private PropertyResBean propertyResBean;
    private TextView tv_pay_this_year;
    private TextView tv_history_arrearage;
    private PropertyResBean.PropertyData propertyData;
    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_property_fee);

        initWidget();

        setUserData();

        getPayInfo();

    }

    /**
     * 获取缴费信息
     */
    private void getPayInfo() {

        HttpUtil.getInstance().setGetRequest("获取中...", this,
                Constant.WEB_ROOT + Constant.GETPROPERTYFEE + "?token=" + sp.getString(Constant.token, "") + "&systemType=" + Constant.SYSTEM_TYPE + "&houseId=" + sp.getString(Constant.houseId, ""), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

//                        LogUtil.e("TAG", "物业欠费信息====" + str);

                        try {

                            PropertyResBean resBean = JSONObject.parseObject(str, PropertyResBean.class);

                            if (Constant.STATUS_SUCESS.equals(resBean.status)) {

                                propertyResBean = JSONObject.parseObject(str, PropertyResBean.class);

                                if (Constant.STATUS_SUCESS.equals(propertyResBean.status)) {

                                    propertyData = propertyResBean.data;

                                    //当年应缴
                                    tv_pay_this_year.setText(propertyData.propertyFee + "");
                                    //历史欠费
                                    tv_history_arrearage.setText(propertyData.historyFee + "");

                                } else {
                                    showToast(propertyResBean.message);
                                }

                            } else {
                                showToast(resBean.message);
                            }

                        } catch (Exception e) {
                            showToast("物业费获取失败");
                        }

                    }
                }, null);

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.pay));

        iv_left.setOnClickListener(this);


        tv_pay_community = (TextView) findViewById(R.id.tv_pay_community);
        tv_roomnum = (TextView) findViewById(R.id.tv_roomnum);

        et_contact = (EditText) findViewById(R.id.et_contact);
        et_contact_tel = (EditText) findViewById(R.id.et_contact_tel);

        iv_personal = (ImageView) findViewById(R.id.iv_personal);
        iv_unit = (ImageView) findViewById(R.id.iv_unit);

        et_unit_name = (EditText) findViewById(R.id.et_unit_name);
//        Button btn_next = (Button) findViewById(R.id.btn_next);

        tv_pay_this_year = (TextView) findViewById(R.id.tv_pay_this_year);
        tv_history_arrearage = (TextView) findViewById(R.id.tv_history_arrearage);
        LinearLayout ll_this_year = (LinearLayout) findViewById(R.id.ll_this_year);
        LinearLayout ll_history_arrearage = (LinearLayout) findViewById(R.id.ll_history_arrearage);

        iv_personal.setOnClickListener(this);
        iv_unit.setOnClickListener(this);

//        btn_next.setOnClickListener(this);
        ll_this_year.setOnClickListener(this);
        ll_history_arrearage.setOnClickListener(this);

    }

    /**
     * 设置用户数据
     */
    private void setUserData() {

        tv_pay_community.setText(sp.getString(Constant.communityName, ""));//小区
        tv_roomnum.setText(sp.getString(Constant.projectName, "") + sp.getString(Constant.buildingNo, "")
                + sp.getString(Constant.unitNo, "") + sp.getString(Constant.houseNo, ""));//房号
        et_contact.setText(sp.getString(Constant.userName, ""));
        et_contact_tel.setText(sp.getString(Constant.mobile, ""));

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

            case R.id.iv_personal://个人发票

                iv_personal.setSelected(!iv_personal.isSelected());
                iv_unit.setSelected(false);

                break;
            case R.id.iv_unit://单位发票

                iv_unit.setSelected(!iv_unit.isSelected());
                iv_personal.setSelected(false);

                break;
//            case R.id.btn_next://下一步

//                nextStep();

//                break;

            case R.id.ll_this_year://当年应缴


                payThisYear();

                break;

            case R.id.ll_history_arrearage://历史欠费


                payHistory();

                break;

        }

    }

    /**
     * 历史欠费
     */
    private void payHistory() {


        if (CommonUtils.isNull(et_contact.getText().toString().trim())) {
            showToast("联系人不能为空");
            return;
        }

        if (CommonUtils.isNull(et_contact_tel.getText().toString().trim())) {
            showToast("联系电话不能为空");
            return;
        }


        //发票类型
        if (iv_unit.isSelected()) {
            if (CommonUtils.isNull(et_unit_name.getText().toString().trim())) {
                showToast("请输入单位名称");
                return;
            }
        }

        String invoiceType = getInvoiceType();

        Intent mIntent = new Intent(this, PayHistoryActivity.class);
        mIntent.putExtra("contact", et_contact.getText().toString().trim());
        mIntent.putExtra("invoiceType", invoiceType);
        mIntent.putExtra("phone", et_contact_tel.getText().toString().trim());
        mIntent.putExtra("unitName", et_unit_name.getText().toString().trim());
        mIntent.putExtra("propertyData", (Serializable) propertyData);
        startActivity(mIntent);

    }

    /**
     * 当年缴费
     */
    private void payThisYear() {

        if (CommonUtils.isNull(et_contact.getText().toString().trim())) {
            showToast("联系人不能为空");
            return;
        }

        if (CommonUtils.isNull(et_contact_tel.getText().toString().trim())) {
            showToast("联系电话不能为空");
            return;
        }

        //发票类型
        if (iv_unit.isSelected()) {
            if (CommonUtils.isNull(et_unit_name.getText().toString().trim())) {
                showToast("请输入单位名称");
                return;
            }
        }

        String invoiceType = getInvoiceType();

        Intent mIntent = new Intent(this, PaymentDetailActivity.class);
        mIntent.putExtra("contact", et_contact.getText().toString().trim());
        mIntent.putExtra("invoiceType", invoiceType);
        mIntent.putExtra("phone", et_contact_tel.getText().toString().trim());
        mIntent.putExtra("unitName", et_unit_name.getText().toString().trim());
        mIntent.putExtra("propertyData", (Serializable) propertyData);
        startActivity(mIntent);

    }

    /**
     * 获取支付类型
     * @return
     */
    private String getInvoiceType() {

        //发票类型, 0是个人 1 是单位 -1是不需要发票
        String invoiceType = "";

        //发票类型
        if (iv_unit.isSelected())
            invoiceType = "1";
        else if (iv_personal.isSelected())
            invoiceType = "0";
        else
            invoiceType = "";

        return invoiceType;
    }
}
